package org.example.chat.server;

import java.util.*;

/**
 * @author : hmxchen
 * @program : modules-api
 * @description :
 * @create : 2022-04-26 12:01
 **/
public class ManageServerThreadConnectClient {

    private String userId;
    private static ServerThreadConnectClient serverThreadConnectClient;

    public ManageServerThreadConnectClient() {
    }

    public ManageServerThreadConnectClient(String userId, ServerThreadConnectClient serverThreadConnectClient) {
        this.userId = userId;
        ManageServerThreadConnectClient.serverThreadConnectClient = serverThreadConnectClient;
    }

    static HashMap<String,ServerThreadConnectClient> onlineUsers = new HashMap<>();

    //add
    public void addServerThreadConnectClient(String userId,ServerThreadConnectClient thread){
        onlineUsers.put(userId,thread);
    }

    //delete
    public void delServerThreadConnectClient(String userId){
        onlineUsers.remove(userId);
    }

    //get
    public ServerThreadConnectClient queryServerThreadConnectClient(String userId){
        return onlineUsers.get(userId);
    }

    //查询全部的用户id对应的在线用户
    public Map<String,ServerThreadConnectClient> getAll(){
        return onlineUsers;
    }

    //查询全部的在线用户线程，每个线程代表一个用户
    public List<ServerThreadConnectClient> getAllUsers(){
        List<ServerThreadConnectClient> onlines = new ArrayList<>();
        Set<String> userIds = onlineUsers.keySet();
        for (String userId : userIds) {
            ServerThreadConnectClient serverThreadConnectClient = onlineUsers.get(userId);
            onlines.add(serverThreadConnectClient);
        }
        return onlines;
    }

    //查询全部的在线用户的 userIds 字符串
    public String getAllUserIds(){
        StringBuffer buffer = new StringBuffer();
        Set<String> userIds = onlineUsers.keySet();
        for (String id : userIds) {
            buffer.append(id+" ");
        }
        return buffer.toString();
    }

    public String onlineUserss(){
        String onlineUserss = " ";
        Iterator<String> iterator = onlineUsers.keySet().iterator();
        while (iterator.hasNext()){
            String userId = iterator.next();
            onlineUserss += userId + " ";
        }
        return onlineUserss;
    }

}
